<?php
include_once "header.php";
session_start();
$id_user=$_SESSION['id_user'];
$email=$_SESSION['email'];

// This is used to submit new markers for review.
// Markers won't appear on the map until they are approved.

function datecheck($input,$format="ymd")
    {
        $separator_type= array(
            "/",
            "-",
            "."
        );
        foreach ($separator_type as $separator) {
            $find= stripos($input,$separator);
            if($find<>false){
                $separator_used= $separator;
            }
        }
		if($separator_used != ""){
	        $input_array= explode($separator_used,$input);
	        if ($format=="mdy") {
	            return checkdate($input_array[0],$input_array[1],$input_array[2]);
	        } elseif ($format=="ymd") {
	            return checkdate($input_array[1],$input_array[2],$input_array[0]);
	        } else {
	            return checkdate($input_array[1],$input_array[0],$input_array[2]);
	        }
	        $input_array=array();
		}	
		return false;
    }


$title = parseInput($_POST['title']);
$type = parseInput($_POST['type']);
$address = parseInput($_POST['address']);
$uri = parseInput($_POST['uri']);
$description = parseInput($_POST['description']);
$start_date = parseInput($_POST['start_date']);
$end_date = parseInput($_POST['end_date']);
$today=date("Y-m-d");
$hashtag = parseInput($_POST['hashtag']);

// validate fields
if(empty($title) || empty($type) || empty($address) || empty($description) || empty($start_date) || empty($end_date)) {
  echo "Se requieren al menos los campos obligatorios.";
  exit;
} elseif(datecheck($start_date)===false) {
    echo "La fecha de inicio no es correcta";
    exit;
} elseif(datecheck($end_date)===false) { 
    echo "La fecha de fin no es correcta";
    exit;
} elseif($today>$start_date) {
  	 echo "La fecha de hoy es mayor a la del inicio de la protesta";
    exit;
} elseif($start_date > $end_date) {
  	 echo "Error, fecha de inicio mayor a la de fin.";
  	 exit;
} else {  

    // insert into db, wait for approval
    $insert = mysql_query("INSERT INTO places (id_user, approved, title, type, address, uri, description, owner_email, start_date, end_date, hashtag) VALUES ('$id_user', 2, '$title', '$type', '$address', '$uri', '$description', '$email', '$start_date', '$end_date', '$hashtag')") or die(mysql_error());

    // geocode new submission
    $hide_geocode_output = true;
    include "geocode.php";
    
    echo "success";
    exit;  
}


?>
